#include "SignalClass.h"
//#include "..\signalConfigDialog.h"

const int W1 = 5;
const int W2 = 1;

SignalClass::SignalClass(void)
{
	data2Plot = dataRecorded= NULL;
	DetrendOn = false;
	cursor=0;
	gCursor=0;
	gEndCursor=gCursor + 20;
	ColorLabel = video::SColor(255,255,255,255);
}

SignalClass::~SignalClass(void)
{
	//Free memory
	if(dataRecorded)	delete [] (dataRecorded);
	if(data2Plot)		delete [] (data2Plot);	
}
void SignalClass::Configure()
{
	Offset = Acc = 0;
/*	SignalConfigDialog Dia(NULL);
	//Dia.tTime->Enable(false);
	Dia.Layout();
	Dia.SetTitle(Label);
	Dia.Configure(this);*/
}

void SignalClass::SetSignal(float mindyn, float maxdyn, float gain, irr::video::SColor color, wxString label, wxString unitLabel, int lunGraph, int SecLength, int Frequency)
{
	lGraph = lunGraph;
	secLength = SecLength;
	frequency = Frequency;
	lRecord = secLength*frequency;
	Color = color;
	Label = label;
	UnitLabel = unitLabel;
	Gain = gain;
	Dyn = maxdyn-mindyn;
	MaxDyn = maxdyn/gain;
	MinDyn = mindyn/gain;
	//data2Plot = new float[lGraph];
	data2Plot = (float*) calloc (lGraph,sizeof(float));
//	dataRecorded = new float[lRecord];
	dataRecorded = (float*) calloc (lRecord,sizeof(float));
	memset(data2Plot,0,sizeof(data2Plot));
	memset(dataRecorded,0,sizeof(dataRecorded));
	/*for (int i=0; i<lGraph; i++)
		data2Plot[i] = 0;
	for (int i=0; i<lRecord; i++)
		dataRecorded[i] = 0;*/
}

void SignalClass::SetDetrend (bool Val)
{
	DetrendOn = Val;
	Offset = 0;
}
bool SignalClass::GetDetrend ()
{
	return DetrendOn;
}

void SignalClass::Remap(void)
{
	int i;
	int xg,sub;
	float mean=0;

if (lRecord>=lGraph)
{
	xg = floor((float)gCursor*(float)lRecord/(float)lGraph);
	sub = ceil((float)lRecord/(float)lGraph);
	
	if (cursor==(xg+sub))
	{
		mean=0;
		for (i=xg; i<(xg+sub); i++)
		{
			//Media Pesata
			/*if ((i-xg)%(2)==1)
				mean+= dataRecorded[i]*W1;
			else
				mean+= dataRecorded[i]*W2;*/
			//Media
				mean+= dataRecorded[i];
		}
		mean=mean/sub;
		//mean=mean/(W1+W2*2);
		data2Plot[gCursor] = mean * Gain/(Dyn/2) ;
		//data2Plot[gCursor] = dataRecorded[i-1]*Gain/Dyn;
		gCursor++;
		gEndCursor++;
	}
}
else
{
	for (i=0; i<lGraph; i++)
		data2Plot[i]=dataRecorded[i*lRecord/lGraph] *Gain/(Dyn/2);
	gCursor = cursor*lGraph/lRecord;
}
	if (cursor>=lRecord)
		cursor=0;
	if (gCursor>=lGraph)
		gCursor=0;
	if (gEndCursor>=lGraph)
		gEndCursor=0;
	//delete [] pConv;
	
}

void SignalClass::AddData(float data)
{
	Acc += data/(lRecord/5);
	if((cursor % (lRecord/5)) == 0)
	{
		Offset = Acc;
		Acc = 0;
	}
	dataRecorded[cursor]=data-(MaxDyn+MinDyn)/2;
	if(DetrendOn)dataRecorded[cursor]-=Offset;
	cursor++;
	Remap();
}

void SignalClass::ResizeTo(int NewLen)
{
	int xg,sub;
	float mean;
	int tCursor=0;
	int tgCursor;
	int i;

	tgCursor = floor((float)gCursor*(float)NewLen/(float)lGraph);
	gCursor = 0;
	lGraph = NewLen;
	delete [] data2Plot;
	data2Plot = new float[lGraph];
	memset(data2Plot,0,sizeof(float)*lGraph);
	
	do{
		xg = floor((float)gCursor*(float)lRecord/(float)lGraph);
		sub = ceil((float)lRecord/(float)lGraph);
		if (tCursor==(xg+sub))
		{
			mean = 0;
			for (i=xg; i<(xg+sub); i++)
			{
				//Media Pesata
				/*if ((i-xg)%(2)==1)
					mean+= dataRecorded[i]*W1;
				else
					mean+= dataRecorded[i]*W2;*/
				//Media
				mean+= dataRecorded[i];
			}
			mean=mean/sub;
			//mean=mean/(W1+W2*2);
			data2Plot[gCursor] = mean * Gain/(Dyn/2) ;
			//data2Plot[gCursor] = dataRecorded[i-1]*Gain/Dyn;
			gCursor++;
		}
		tCursor++;
	}while(tCursor<cursor);
	gCursor=tgCursor;
	gEndCursor = gCursor +20;
	if (gCursor>=lGraph)	gCursor=0;
	if (gEndCursor>=lGraph)		gEndCursor-=lGraph;
	cursor = gCursor*lRecord/lGraph;

	//gCursor=0;
	//delete [] data2Plot;
	//lGraph=NewLen;
	//data2Plot = new float[lGraph];
	//memset(data2Plot,0,sizeof(float)*lGraph);
	//gEndCursor = gCursor+20;
	//cursor=0;
}